
/* global getCookie visitUrl*/
var userInfo = {};
$(function () { 
    $.get('/antique/api/sysUser/query_my_info', { token:getCookie('token') }, function (result) {
        if (result.code == 0) {
            var data = result.data;
            userInfo = data;
            if(userInfo.customSignature){
                $('#sign').text(userInfo.customSignature);
            } else {
                $('#sign').text('我还没有个性签名');
            }
            $(".userName").html(data.name);
            if (data.photo) {
                $(".userPhoto").attr("src",data.photo); 
            } else {
                $(".userPhoto").attr("src",'');
            }
            if (data.shopDetail && data.shopDetail.id){
                // $(".weui-navbar__item").removeClass("weui-bar__item--on");
                // $("#shopCenter").addClass("weui-bar__item--on");
                // $(".weui-tab__bd-item").removeClass("weui-tab__bd-item--active");
                // $("#tab2").addClass("weui-tab__bd-item--active");
                
                $(".shopName").html(data.shopDetail.name);                
                $(".shopPhoto").attr('style','background: url(' + data.shopDetail.image +'?x-oss-process=image/resize,w_400) no-repeat 0 center;background-size: 100% auto;margin-top:0.5rem;'); 
            } else { 
                if (jump) { 
                    visitUrl('/shop/add', true);
                    return;
                }
                $("#shopCenter").attr("href","javascript:void(0);");  
            }
            if (data.shopDetail && data.shopDetail.id){
                $.get('/antique/api/mallShop/web/query', { token:getCookie('token') ,id:data.shopDetail.id }, function (result) {
                    if (result.code == 0) {
                        var data = result.data;
                        $("#goodEvaluationRate").html(data.goodEvaluationRate + "%");
                        $("#saleCount").html(data.saleCount);
                        $("#collectionCount").html(data.collectionCount);
                    }
                });
            }
            
            //查询店铺积分信息
            $.post('/antique/api/mallShop/web/query',{
                id:data.shopDetail.id
            },function(result){
                var level = 0;
                var creditScore = result.data.creditScore;
                //通过积分计算用户等级
                if(creditScore >= 0 && creditScore <= 20) {
                    level = 1;
                } else if(creditScore >= 21 && creditScore <= 50){
                    level = 2;
                } else if(creditScore >= 51 && creditScore <= 100){
                    level = 3;
                } else if(creditScore >= 101 && creditScore <= 200){
                    level = 4;
                } else if(creditScore >= 201 && creditScore <= 500){
                    level = 5;
                } else if(creditScore >= 501 && creditScore <= 1000){
                    level = 6;
                } else if(creditScore >= 1001 && creditScore <= 1500){
                    level = 7;
                } else if(creditScore >= 1501 && creditScore <= 2000){
                    level = 8;
                } else if(creditScore >= 2001 && creditScore <= 5000){
                    level = 9;
                } else if(creditScore >= 5001){
                    level = 10;
                }
                $('#shopLevel').attr('src','/img/shop/level' + level +'.png');
            });

            $.post('/antique/api/productBidLog/query_bid_total',{
                token:getCookie('token'),
                shopId:data.shopDetail.id,
                bidType:2
            },function(result){
                var data = result.data;
                if(data.state_0 && data.state_0 != 0)
                    $('#shopOffer').text(data.state_0?data.state_0:0).show();
            });
        }
    });

    $.get('/antique/api/sysUser/query_my_wallet', { token : getCookie('token') }, function (result) {
        if ( result.code == 0) {
           $("#balance").html(result.data.balance);
        }        
    });
    //查询积分
    getIntegral();
    //判断是否签到
    $.get('/antique/api/userDaySignIn/check_day_sign_in', { token : getCookie('token') }, function (result) {
        if ( result.code == 0) {
          if (result.data.daySignIn) {
            $(".my-top-sign").html("已签到");
          }
        }        
    });

});

 $("#shopCenter").click(function () {
    if (userInfo && userInfo.shopDetail && userInfo.shopDetail.id) {
        //有店铺
    } else {
        //没有店铺
        visitUrl('/shop/add',true);
    }
 });
 $(".my-top-sign").click(function () {
    $.get('/antique/api/userDaySignIn/day_sign_in', { token : getCookie('token') }, function (result) {
        if ( result.code == 0) {
            $.toast("签到成功！");
            $(".my-top-sign").html("已签到");
            getIntegral();
        }
        
    });
 });

 function getIntegral(){
    $.get('/antique/api/userCoin/query_my_coin', { token : getCookie('token') }, function (result) {
        if ( result.code == 0) {
           $("#integral").html(result.data.coin);
        }
        
    });
 }

 $("#goFollowUser").click(function () {
    visitUrl('/shop/followUser?shopId=' + userInfo.shopDetail.id,true);
});

//编辑个性签名
$('#editSign').click(function(){
    $('textarea[name="sign-content"]').val('');
    $("#mySign").popup();
});

$('#confirmSign').click(function(){
    var text = $('textarea[name="sign-content"]').val();
    if(text.length >= 4 && text.length <= 12){
         $.post('/antique/api/sysUser/update_detail', {
             token:getCookie('token'),
             customSignature:text
         },function(data){
             $('#sign').text(text);
         });
    } else {
         $.toast('对不起，您输入的举报内容文字不在4-12个字内！请修改后上传。');
    }
 });

 $('textarea[name="sign-content"]').keyup(function(e){
    var text = $(this).val();
    $('.weui-textarea-counter span').text(text.length);
    if(text.length < 20){
        $('.weui-textarea-counter span').removeClass('green').addClass('red');
    } else if(text.length > 200){
        $('.weui-textarea-counter span').removeClass('green').addClass('red');
    } else {
        $('.weui-textarea-counter span').removeClass('red').addClass('green');
    }
});

$.post('/antique/api/mallOrder/user/query_state_total',{
    token:getCookie('token')
},function(result){
    var data = result.data;
    if(data.state_0 && data.state_0 != 0)
        $('#userWaitPay').text(data.state_0?data.state_0:0).show();
    if(data.state_10 && data.state_10 != 0)
        $('#userWaitSend').text(data.state_10 ?data.state_10 :0).show();
    if(data.state_20 && data.state_20 != 0)
        $('#userWaitReceiving').text(data.state_20 ?data.state_20 :0).show();
    if(data.state_30 && data.state_30 != 0)
        $('#userWaitEvaluate').text(data.state_30 ?data.state_30 :0).show();
    if(data.state_31 && data.state_31 != 0)
        $('#userWaitAfterSale').text(data.state_31 ?data.state_31 :0).show();
});

$.post('/antique/api/productBidLog/query_bid_total',{
    token:getCookie('token'),
    userId:getCookie('userId'),
    bidType:2
},function(result){
    var data = result.data;
    if(data.state_1 && data.state_1 != 0)
        $('#userOffer').text(data.state_1?data.state_1:0).show();
});

$.post('/antique/api/productBidLog/query_bid_total',{
    token:getCookie('token'),
    userId:getCookie('userId'),
    bidType:1
},function(result){
    var data = result.data;
    if(data.state_0 && data.state_0 != 0)
        $('#userPaimai').text(data.state_0?data.state_0:0).show();
});

$.post('/antique/api/mallOrder/shop/query_state_total',{
    token:getCookie('token')
},function(result){
    var data = result.data;
    if(data.state_10 && data.state_10 != 0)
        $('#shopWaitSend').text(data.state_10 ?data.state_10 :0).show();
    if(data.state_30 && data.state_30 != 0)
        $('#shopWaitEvaluate').text(data.state_30   ?data.state_30   :0).show();
    if(data.state_31 && data.state_31 != 0)
        $('#shopWaitAfterSale').text(data.state_31  ?data.state_31  :0).show();
});

//设置用户积分
$.post('/antique/api/userCreditScore/query_my_credit_score',{
    token:getCookie('token')
},function(result){
    var level = 0;
    var levelDesc = '';
    var creditScore = (result.data && result.data.creditScore) ? result.data.creditScore : 0;
    //通过积分计算用户等级
    if(creditScore < 0) {
        level = 1;
        levelDesc = '无品';
    } else if(creditScore >= 0 && creditScore <= 150){
        level = 1;
        levelDesc = '无品';
    } else if(creditScore >= 151 && creditScore <= 500){
        level = 2;
        levelDesc = '一品官';
    } else if(creditScore >= 501 && creditScore <= 1000){
        level = 3;
        levelDesc = '二品官';
    } else if(creditScore >= 1001 && creditScore <= 1500){
        level = 4;
        levelDesc = '三品官';
    } else if(creditScore >= 1501 && creditScore <= 2000){
        level = 5;
        levelDesc = '四品官';
    } else if(creditScore >= 2001 && creditScore <= 2500){
        level = 6;
        levelDesc = '五品官';
    } else if(creditScore >= 2501 && creditScore <= 5000){
        level = 7;
        levelDesc = '六品官';
    } else if(creditScore >= 5001 && creditScore <= 10000){
        level = 8;
        levelDesc = '七品官';
    } else if(creditScore >= 10001 && creditScore <= 50000){
        level = 9;
        levelDesc = '八品官';
    } else if(creditScore >= 50001){
        level = 10;
        levelDesc = '九品官';
    }
    $('.my-top .my-top-left .level').attr({
        style:'background:url(/img/user/level' + level +'.png) center center no-repeat;background-size:100% auto;'
    });
    $('#levelDesc').text(levelDesc);
});

//查询热推轮播广告
$.post('/antique/api/mallAdvInfo/web/query_list',{
    type:1,
    start:0,
    limit:1
},function(result){
    $('#my-ad').append(template('my-ad-list', {
        list:result.data.list
    }));
});

//查询热推轮播广告
$.post('/antique/api/mallAdvInfo/web/query_list',{
    type:3,
    start:0,
    limit:1
},function(result){
    $('#my-shop-ad').append(template('my-shop-ad-list', {
        list:result.data.list
    }));
});

